<template>
    <div class="supplier">
        <!-- 耗材供应商 -->
        <serchForm :Form="inventoryForm" :FormLabel="inventoryFormLabel" @onSearch="onSearch" />
        <div class="function-box" v-if="checkPermi(['system:supplier:edit'])">
            <el-button class="createBtn" @click="addVendor"><i class="iconfont icon-plus-square" />新增</el-button>

            <el-button class="exportBtn" @click="deleteData"><i class="iconfont icon-shanchu" />删除</el-button>
        </div>
        <forTableCom :color="color" :idActive="idActive" :bg="bg" :total="totals" :page="pageNum" 
            :limit="pageSize" @handleSelectionChange="handleSelectionChange" @handleChange="handleChange"
            @updatePage="updatePage" @updatepageSize="updatepageSize" :tableHeaderData="inventoryTableHeaderData"
            :tableData="inventoryTableData" @tableDetail="tableDetail">
        </forTableCom>
        <venderPop v-if="flag" :popTitle="popTitle" :disabled="disabled" :PopData="PopData" :defindFlag="defindFlag" @popUpsFlase="popUpsFlase" @getoutboundOrderData="getoutboundOrderData"/>
    </div>
</template>
<script setup>
import serchForm from '@/components/inventory/serchForm.vue';
import forTableCom from '@/components/forTableCom/index.vue';
import { ElMessage } from 'element-plus'
import venderPop from './components/venderPop';
import { updatePopHook } from '@/hook/setPop/updatePop.js'
import { querySelectSupplierApi,deleteSupplierApi, } from '@/api/inventory'
import { deletePopHook } from '@/hook/setPop/deletePop.js';
import { checkPermi } from "@/utils/permission.js";

let updatePop = updatePopHook()
let deletePop = deletePopHook()
import { ref } from 'vue'
const idActive = 'supplierId'
let flag = ref(false)
let popTitle = ref('新增耗材供应商')
let disabled = ref(false)
let color = ref('#333')
let bg = ref('#d9d9d93b')
// 修改详情回显数据弹窗
let PopData = ref([])
// 详情控制变量
let defindFlag = ref(true)
// 表单
let inventoryForm = ref(
    {
        supplierId: '',           // 供应商编号
        supplierName: '',         // 供应商名称
        responsiblePerson: '',  // 经办人
        contactPhone: '',       // 联系电话
    }
)
// 表单数据标题
let inventoryFormLabel = ref([

    {
        name: 'supplierId',
        label: '供应商编号'
    },
    {
        name: 'supplierName',
        label: '供应商名称'
    },
    {
        name: 'responsiblePerson',
        label: '经办人'
    },
    {
        name: 'contactPhone',
        label: '联系电话'
    },
])
// 查找数据
const onSearch = (v) => {
    // 每次查询后回到页1
    pageNum.value = 1
    querySelectSupplier(v)
}
// 删除勾选的列二次提示
const deleteData = () => {
    let fn = async () => {
        let params = {
            supplierId: multipleSelection.value[0].supplierId
        }
        let res = await deleteSupplierApi(params)
        let { code, msg } = res
        if (code == 200) {
            getoutboundOrderData()
            ElMessage({
                message: msg,
                type: 'success',
            })
        }
    }
    deletePop(multipleSelection, fn)
}

// 总条目数   pageSize 加 1
let totals = ref(50)
// 当前页数
let pageNum = ref(1)
// 条数
let pageSize = ref(50)
// 存储勾选框的对象
let multipleSelection = ref([])
// 表格表头数据
let inventoryTableHeaderData = ref([
    {
        prop: 'supplierId',
        label: '供应商编号',
        colorClassName: 'activeColor'
    },
    {
        prop: 'supplierName',
        label: '供应商名称',
        colorClassName: 'activeColor'
    },
    {
        prop: 'responsiblePerson',
        label: '经办人'
    },
    {
        prop: 'contactPhone',
        label: '联系电话'
    },
    {
        prop: 'supplierAddress',
        label: '供应商地址',
    },
    {
        prop: 'cooperativeStatus',
        label: '合作状态',
    },
    {
        prop: 'remarks',
        label: '备注',
    },
])
// 表格数据内容
let inventoryTableData = ref([
  
])
// 选中的列
const handleSelectionChange = (v) => {
    multipleSelection.value = v
    console.log(v);
}
// 分页器大小  当前分页器
const handleChange = (v) => {
    let { page, limit } = v.page
    pageNum.value = page
    pageSize.value = limit
    let obj = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
    }
    let params = Object.assign(obj,inventoryForm.value[0])
    querySelectSupplier(params)
}
// 当前页面位置发生变化
const updatePage = (v) => {
    console.log('页面位置发生变化', v);
}
// 条目个数发生变化
const updatepageSize = (v) => {
    console.log('条目个数发生变化', v);
}
// 打开新增弹窗
const addVendor = () => {
    flag.value = true
    disabled.value = false
    popTitle.value = '新增耗材供应商'
}
// 打开修改弹窗
const updateVendor = () => {
    updatePop(multipleSelection, flag, popTitle, '修改耗材供应商', disabled)
}
// 详情双击行
const tableDetail = (props) => {
    disabled.value = true
    flag.value = true
    popTitle.value = '耗材供应商详情信息'
    defindFlag.value = false
    PopData.value = props
}
// 关闭弹窗
const popUpsFlase = (props) => {
    defindFlag.value = true
    flag.value = props
}
// 查询入库单数据
const querySelectSupplier = async (data = '') => {
    let res = await querySelectSupplierApi(data)
    let { code, rows, total } = res
    if (code == 200) {
        inventoryTableData.value = rows 
        totals.value = total
        console.log(rows);
    }
}
const getoutboundOrderData = ()=>{
    let params = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
    }
    querySelectSupplier(params)
}
onMounted(() => {
    getoutboundOrderData()
})
</script>
<style scoped lang='scss'>
.supplier {
    padding: 16px;
}
</style>
